package com.xinai.adspter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


import com.xinai.R;
import com.xinai.util.Tools;

import java.util.List;
import java.util.Map;

public class DisplayListAdapter extends BaseAdapter {

    private List<Map<String, Object>> data;
    private LayoutInflater layoutInflater;
    private Context context;

    public DisplayListAdapter(Context context, List<Map<String,Object>> data){
        this.context=context;
        this.data =data;
        this.layoutInflater = LayoutInflater.from(context);
    }
    /*
    * 组件类
    * */
   public final class Display{
        public   TextView productNo;
        public  TextView colorName;
        public  TextView sizesName;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void refresh() {
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Display display =null;
        if(convertView ==null){ //获取组件 convertView  就是整个布局文件 ，一个View
            display =new Display();
            convertView =layoutInflater.inflate(R.layout.displaylistitem,null);
            display.productNo =(TextView) convertView.findViewById(R.id.productNo);
            display.colorName =(TextView) convertView.findViewById(R.id.colorName);
            display.sizesName =(TextView)convertView.findViewById(R.id.sizesName);
            convertView.setTag(display);
        }else {
            display = (Display) convertView.getTag();
        }

        // 绑定数据
        // 显示货品编码(控制长度)
        String goodsCode = String.valueOf(data.get(position).get("productNo"));
        int length = Tools.length(goodsCode);
        if (length > 26) {
            display.productNo.setTextSize(13);
        } else {
            display.productNo.setTextSize(17);
        }
        display.productNo.setText(goodsCode);
        display.colorName.setText(String.valueOf(data.get(position).get("colorName")));
        display.sizesName.setText(String.valueOf(data.get(position).get("sizesName")));
        return convertView;
    }
}
